# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=shiboken2
pkgbase=mingw-w64-${_realname}-qt5
pkgname=(${MINGW_PACKAGE_PREFIX}-${_realname}-qt5 ${MINGW_PACKAGE_PREFIX}-python{2,3}-${_realname}-qt5)
pkgver=5.11.0
pkgrel=1
arch=('any')
url="https://doc-snapshots.qt.io/qtforpython/"
license=("LGPL")
source=(https://download.qt.io/official_releases/QtForPython/pyside2/PySide2-${pkgver}-src/pyside-setup-everywhere-src-${pkgver}.tar.xz
        001-shiboken.patch
        002-cmake-relative-paths.patch
        003-mingw-adjust-flags.patch)
sha256sums=('fbc412c4544bca308291a08a5173a949ca530d801f00b8337902a5067e490922'
            '7fbf1140d6c63b6c1a6f621b800654236dd5e8b0ff0bbd42c5e5940a51f0052d'
            'da64af18fbc52e34eed454287de01cecebf069354d2a206f172dad329065fdd6'
            '3acdc6da5bc063256dce0ca31ed4dee2f35beb734792675a4c2bafdb2e365019')
makedepends=("${MINGW_PACKAGE_PREFIX}-clang"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-libxml2"
             "${MINGW_PACKAGE_PREFIX}-libxslt"
             "${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-qt5")
options=('staticlibs' 'strip')

prepare() {
  cd ${srcdir}/pyside-setup-everywhere-src-${pkgver}
  patch -p1 -i ${srcdir}/001-shiboken.patch
  patch -p1 -i ${srcdir}/002-cmake-relative-paths.patch
  patch -p1 -i ${srcdir}/003-mingw-adjust-flags.patch
}

build() {
  for pyv in python2 python3; do
    [[ -d ${srcdir}/build-${MINGW_CHOST}-${pyv} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}-${pyv}
    mkdir -p ${srcdir}/build-${MINGW_CHOST}-${pyv} 
    pushd ${srcdir}/build-${MINGW_CHOST}-${pyv} > /dev/null

    MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
    ${MINGW_PREFIX}/bin/cmake \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_C_COMPILER=${MINGW_PREFIX}/bin/clang.exe \
      -DCMAKE_CXX_COMPILER=${MINGW_PREFIX}/bin/clang++.exe \
      -DCMAKE_BUILD_TYPE=Release \
      -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/${pyv}.exe \
      ../pyside-setup-everywhere-src-${pkgver}/sources/shiboken2

    make

    popd > /dev/null
  done

}

fix_paths() {
  local prefixwin=$(cygpath -am ${MINGW_PREFIX})
  local prefixinst=$(cygpath -am ${pkgdir}${MINGW_PREFIX})

  local _files=$(find ${pkgdir}${MINGW_PREFIX}/lib -type f -iname "*.cmake" -o -iname "*.pc")
  for f in ${_files}; do
    plain "Patching ${f}..."
    sed -e "s|${prefixwin}|${MINGW_PREFIX}|g" \
        -e "s|${prefixinst}|${MINGW_PREFIX}|g" \
        -i ${f}
  done
}

package_shiboken() {
  pkgdesc="CPython bindings generator for C++ libraries (mingw-w64)"
  makedepends=("${MINGW_PACKAGE_PREFIX}-clang"
               "${MINGW_PACKAGE_PREFIX}-libxml2"
               "${MINGW_PACKAGE_PREFIX}-libxslt"
               "${MINGW_PACKAGE_PREFIX}-python3"
               "${MINGW_PACKAGE_PREFIX}-qt5")

  cd ${srcdir}/build-${MINGW_CHOST}-python3
  make DESTDIR=${pkgdir} install

  rm ${pkgdir}${MINGW_PREFIX}/bin/*.dll
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/python*
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/libshiboken*
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig
  rm ${pkgdir}${MINGW_PREFIX}/lib/cmake/Shiboken2-${pkgver}/Shiboken2Config*python*.cmake

  fix_paths
}

package_python2-shiboken() {
  pkgdesc="Support library for Python2 bindings (mingw-w64)"
  install=${_realname}-${CARCH}.install
  depends=("${MINGW_PACKAGE_PREFIX}-python2"
           "${MINGW_PACKAGE_PREFIX}-shiboken2-qt5=${pkgver}")

  cd ${srcdir}/build-${MINGW_CHOST}-python2
  make DESTDIR=${pkgdir} install

  mv ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/shiboken2{,-py2}.pc

  rm ${pkgdir}${MINGW_PREFIX}/bin/*.exe
  rm -rf ${pkgdir}${MINGW_PREFIX}/{include,share}
  rm ${pkgdir}${MINGW_PREFIX}/lib/cmake/Shiboken2-${pkgver}/Shiboken2ConfigVersion.cmake
  rm ${pkgdir}${MINGW_PREFIX}/lib/cmake/Shiboken2-${pkgver}/Shiboken2Config.cmake

  fix_paths
}

package_python3-shiboken() {
  pkgdesc="Support library for Python3 bindings (mingw-w64)"
  install=${_realname}-${CARCH}.install
  depends=("${MINGW_PACKAGE_PREFIX}-python3"
           "${MINGW_PACKAGE_PREFIX}-shiboken2-qt5=${pkgver}")

  cd ${srcdir}/build-${MINGW_CHOST}-python3
  make DESTDIR=${pkgdir} install

  rm ${pkgdir}${MINGW_PREFIX}/bin/*.exe
  rm -rf ${pkgdir}${MINGW_PREFIX}/{include,share}
  rm ${pkgdir}${MINGW_PREFIX}/lib/cmake/Shiboken2-${pkgver}/Shiboken2ConfigVersion.cmake
  rm ${pkgdir}${MINGW_PREFIX}/lib/cmake/Shiboken2-${pkgver}/Shiboken2Config.cmake

  fix_paths
}

# Wrappers
package_mingw-w64-i686-shiboken2-qt5() {
  package_shiboken
}

package_mingw-w64-i686-python2-shiboken2-qt5() {
  package_python2-shiboken
}

package_mingw-w64-i686-python3-shiboken2-qt5() {
  package_python3-shiboken
}

package_mingw-w64-x86_64-shiboken2-qt5() {
  package_shiboken
}

package_mingw-w64-x86_64-python2-shiboken2-qt5() {
  package_python2-shiboken
}

package_mingw-w64-x86_64-python3-shiboken2-qt5() {
  package_python3-shiboken
}
